package controllers.common;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import play.mvc.Controller;
import play.utils.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;

public class ControllerBase extends Controller {
    protected static void setCache(int ms) {
        Calendar now = Calendar.getInstance();
        response.setHeader("Expires", Utils.getHttpDateFormatter().format(new Date(now.getTimeInMillis() + ms)));
        response.setHeader("Cache-Control", "private, max-age=100000");
    }

    protected static void renderCachedBinary(File file, String cacheDuration) throws FileNotFoundException {
        InputStream faviconStream = new FileInputStream(file);
        response.setHeader("Content-Length", file.length() + "");
        response.cacheFor(cacheDuration);
        response.contentType = "text/plain";
        response.direct = faviconStream;
    }

    protected static ReCaptchaResponse ValidateCaptcha() {
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey("6LfuQdYSAAAAAP9iJFRmk3uAoLjDmPOAMGLoPUFw");
        return reCaptcha.checkAnswer(request.remoteAddress,
                request.params.get("recaptcha_challenge_field"),
                request.params.get("recaptcha_response_field"));
    }
}
